
* 0. Read in data 

use "${dataanalysis}\KidsReplication.dta", clear
drop if kidattrite == 1

* 1. globals 

* adjusting variable names for randcmd
rename (mangroves_0 manclimate manbiodiv manconsume mangroves_6_1_index) (y1 y2 y3 y4 y5) // knowledge
rename (env_att_2 env_att_3 env_att_5_zero env_att_7_zero  env_att_8_pos env_concern_index2z) (a1 a2 a3 a4 a5 a6) // attitudes
rename (mangroves_3 mangroves_15 mangroves_1_neg mangroves_1_nat mangroves_1_tourism) (m1 m2 m3 m4 m5) // mangrove attitudes
rename  (behavior_2_pos  behavior_3  behavior_4 behavior_6_pos behavior_index_1z) (b1 b2 b3 b4 b5) // behaviors

global envknowledge  y1 y2 y3 y4 y5 
global envatt a1 a2 a3 a4 a5 a6
global manatt m1 m2 m3 m4 m5
global envbehav b1 b2 b3 b4 b5
global summarytable y5 a6 m1 b5 

* 2. Preliminaries

set scheme tab1
xtset id_y

* Flip coding of questions so that all responses indicate positive attitudes towards mangroves, rather than absence of negative attitudes
recode m1 (0=1) (1=0)
recode m2 (0=1) (1=0)

* Generate interactions to facilitate use of randcmd, which does not accept factors
gen treattime = timesincetreatment*treatindividual*post
gen treatgender= dem_gender*treatindividual*post
gen treatage = dem_age*treatindividual*post


* 3. knowledge table
if 0{


preserve
gen yn = 0 // generate another variable as a placement column to add joint p-value for now. 
la var yn "Joint test"


drop if spillsample == 1
estimates clear

/*

RI: average treatment


*/
randcmd ///
((treatindiv) eststo xtr1: xtreg y1 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr2: xtreg y2 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr3: xtreg y3 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr4: xtreg y4 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr5: xtreg y5 treatindiv post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id) reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post)

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,1,.)
forvalues i = 1/5 {
		matrix rivals[`i',1] = rcoef[`i',6]
}

matrix rivals[6,1] = rmult[6,6]
local wy = rmult[6,6]

mat colnames rivals = treatindiv 
mat rownames rivals = y1 y2 y3 y4 y5 yn
matrix list rivals

file open newfile  using "$tables/wyknowledge.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'") 
file close newfile

foreach var in $envknowledge yn {
  eststo xtr_`var': xtreg `var' treatindiv post, fe nonest vce(cluster club_id)
   estadd matrix ri = rivals["`var'",1...]
}

/*

RI: treatment and time


*/
	
randcmd ///
((treatindiv treattime) eststo xtr1: xtreg y1 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr2: xtreg y2 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr3: xtreg y3 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr4: xtreg y4 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr5: xtreg y5 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id) reps(1000)  seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treattime = timesincetreatment*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[6,2] = rmult[6,6]

mat colnames rivals = treatindiv treattime
mat rownames rivals = y1 y2 y3 y4 y5 yn
matrix list rivals

foreach var in $envknowledge yn  {
  eststo xtrtime1_`var': xtreg `var' treatindiv post treattime, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
  
  } 
  
/*

RI:  treatment and gender


*/ 
	
randcmd ///
((treatindiv treatgender) eststo xtr1: xtreg y1 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr2: xtreg y2 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr3: xtreg y3 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr4: xtreg y4 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr5: xtreg y5 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id) reps(10) seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treatgender= dem_gender*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[6,2] = rmult[6,6]


mat colnames rivals = treatindiv treatgender
mat rownames rivals = y1 y2 y3 y4 y5 yn


foreach var in $envknowledge yn {
  eststo xtrgen1_`var': xtreg `var' treatindiv post treatgender, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
	}

	
/*

RI:  treatment and age


*/
randcmd ///
((treatindiv treatage) eststo xtr1: xtreg y1 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr2: xtreg y2 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr3: xtreg y3 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr4: xtreg y4 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr5: xtreg y5 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id) reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treatage = dem_age*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
		
}

matrix rivals[6,2] = rmult[6,6]

mat colnames rivals = treatindiv treatage
mat rownames rivals = y1 y2 y3 y4 y5 yn 	

foreach var in $envknowledge yn {
   
  eststo xtrage1_`var': xtreg `var' treatindiv post treatage, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]
  sum `var' if dem_gender == 0 & treatment == 1 & post == 0
  local ymeanboys = round(r(mean),0.01)
  estadd scalar sb `ymeanboys':  xtrage1_`var'
  sum `var' if dem_gender == 1 & treatment == 1 & post == 0
  local ymeangirls = round(r(mean),0.01)
  estadd scalar sg `ymeangirls':  xtrage1_`var'
  sum `var'
  local obs = r(N) 	
  estadd scalar o1 `obs': xtrage1_`var'
}
 
restore

/*

RI: spillovers


*/

preserve
keep if spillsample == 1 | treatment == 1
g yn = 0 
// Spillovers with heterogeneity

randcmd ///
((control50 control75) eststo xtr1: xtreg y1 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr2: xtreg y2 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr3: xtreg y3 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr4: xtreg y4 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr5: xtreg y5 control50 control75 post, fe nonest vce(cluster club_id)) ///
, treatvars(treat50 treat75) groupvar(club_id) reps(1000) seed(1234) calc1(replace control50 = treat50*post) calc2(replace control75 = treat75*post) 
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[6,2] = rmult[6,6]
local wy = rmult[6,6]

mat colnames rivals = control50 control75
mat rownames rivals = y1 y2 y3 y4 y5 yn 	
matrix list rivals

file open newfile  using "$tables/wyknowspill.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'") 
file close newfile

foreach var in $envknowledge yn {
  eststo xtrspill1_`var': xtreg `var' control50 control75 post, fe nonest vce(cluster club_id) 
  estadd matrix ri = rivals["`var'",1...]
  test control75=control50 =0
   local rp = round(r(p),0.01)
  estadd scalar joint `rp': xtrspill1_`var'
  sum `var' if treatment == 1 & post == 0
  local ymean = round(r(mean),0.01)
  estadd scalar s1 `ymean':  xtrspill1_`var'
  sum `var'
  local obs = r(N)
  estadd scalar o1 `obs':  xtrspill1_`var'

}

restore
		
esttab xtr_y1 xtr_y2 xtr_y3 xtr_y4 xtr_y5 using "$tables/kidsknowledge.tex", replace se nonotes noobs style(tex) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
keep(treatindiv) nolines ///
coeflabels(treatindiv "Treated") nolines collabels(none) ///
		starlevels(* 0.10 ** 0.05 *** 0.01) label nomtitles ///
		nonumbers  fragment  ///
		prehead( ) ///
		posthead(\\ \midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrgen1_y1 xtrgen1_y2 xtrgen1_y3 xtrgen1_y4 xtrgen1_y5 using "$tables/kidsknowledge.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treatgender) nolines  collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatgender "Treated $\times$ Female") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrtime1_y1 xtrtime1_y2 xtrtime1_y3 xtrtime1_y4 xtrtime1_y5 using "$tables/kidsknowledge.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treattime) nolines collabels(none)  ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treattime "Treated $\times$ Time since treated") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\  ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrage1_y1 xtrage1_y2 xtrage1_y3 xtrage1_y4 xtrage1_y5  using "$tables/kidsknowledge.tex", append se nonotes style(tex) noobs scalar("sb Mean control boys" "sg Mean control girls" "o1 Observations") sfmt(%12.3f %12.3f %12.0f) b(%12.3f) se(%12.3f) ///
keep(treatindiv treatage) nolines collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatage "Treated $\times$ Age") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot( \\ \midrule) ///
		postfoot()
		
esttab xtrspill1_y1 xtrspill1_y2 xtrspill1_y3 xtrspill1_y4 xtrspill1_y5  using "$tables/kidsknowledge.tex", append se nonotes style(tex) label ///
 noobs scalar("joint P-val joint test" "s1 Mean baseline" "o1 Observations") sfmt(%12.3f %12.3f %12.0f)  b(%12.3f) se(%12.3f) ///
keep(control50 control75) nolines collabels(none) ///
coeflabels(control50 "Control 50/50" control75 "Control 75/25") ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot(\\ \midrule) ///
		postfoot()
}


		
* 4. Environmental attitudes table
estimates clear 

if 0{		
estimates clear
preserve
drop if spillsample == 1
g an = 0
estimates clear

/*

RI: average treatment


*/
randcmd ///
((treatindiv) eststo xtr1: xtreg a1 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr2: xtreg a2 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr3: xtreg a3 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr4: xtreg a4 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr5: xtreg a5 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr6: xtreg a6 treatindiv post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id) reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post)

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(7,1,.)
forvalues i = 1/6 {
		matrix rivals[`i',1] = rcoef[`i',6]
}

matrix rivals[7,1] = rmult[7,6]
local wy = rmult[7,6]

mat colnames rivals = treatindiv 
mat rownames rivals = a1 a2 a3 a4 a5 a6 an
matrix list rivals

file open newfile  using "$tables/wyatt.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'") 
file close newfile


foreach var in $envatt an {
  eststo xtr_`var': xtreg `var' treatindiv post, fe nonest vce(cluster club_id)
   estadd matrix ri = rivals["`var'",1...]
}

/*

RI: treatment and time


*/
	
randcmd ///
((treatindiv treattime) eststo xtr1: xtreg a1 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr2: xtreg a2 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr3: xtreg a3 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr4: xtreg a4 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr5: xtreg a5 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr6: xtreg a6 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id) reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treattime = timesincetreatment*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(7,2,.)
forvalues i = 1/6 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[7,2] = rmult[7,6]

mat colnames rivals = treatindiv treattime
mat rownames rivals = a1 a2 a3 a4 a5 a6 an
matrix list rivals

foreach var in $envatt an  {
  eststo xtrtime1_`var': xtreg `var' treatindiv post treattime, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
  
  } 
  
/*

RI:  treatment and gender


*/ 
	
randcmd ///
((treatindiv treatgender) eststo xtr1: xtreg a1 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr2: xtreg a2 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr3: xtreg a3 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr4: xtreg a4 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr5: xtreg a5 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr6: xtreg a6 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id) reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treatgender= dem_gender*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(7,2,.)
forvalues i = 1/6 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[7,2] = rmult[7,6]

mat colnames rivals = treatindiv treatgender
mat rownames rivals = a1 a2 a3 a4 a5 a6 an
matrix list rivals
  

foreach var in $envatt an {
  eststo xtrgen1_`var': xtreg `var' treatindiv post treatgender, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
	}

	
/*

RI:  treatment and age


*/
randcmd ///
((treatindiv treatage) eststo xtr1: xtreg a1 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr2: xtreg a2 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr3: xtreg a3 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr4: xtreg a4 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr5: xtreg a5 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr5: xtreg a6 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id) reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treatage = dem_age*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(7,2,.)
forvalues i = 1/6 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
		
}

matrix rivals[7,2] = rmult[7,6]

mat colnames rivals = treatindiv treatage
mat rownames rivals = a1 a2 a3 a4 a5 a6 an

foreach var in $envatt an {
   
  eststo xtrage1_`var': xtreg `var' treatindiv post treatage, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]
  sum `var' if  dem_gender == 0 & treatment == 1 & post == 0
  local ymeanboys = round(r(mean),0.01)
  estadd scalar sb `ymeanboys':  xtrage1_`var'
  sum `var' if dem_gender == 1 & treatment == 1 & post == 0
  local ymeangirls = round(r(mean),0.01)
  estadd scalar sg `ymeangirls':  xtrage1_`var'
  sum `var'
  local obs = r(N) 	
  estadd scalar o1 `obs': xtrage1_`var'
}
 
restore

/*

RI: spillovers


*/

preserve
keep if spillsample == 1 | treatment == 1
g an = 0 
// Spillovers

randcmd ///
((control50 control75) eststo xtr1: xtreg a1 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr2: xtreg a2 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr3: xtreg a3 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr4: xtreg a4 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr5: xtreg a5 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr6: xtreg a6 control50 control75 post, fe nonest vce(cluster club_id)) ///
, treatvars(treat50 treat75) groupvar(club_id) reps(1000) seed(1234) calc1(replace control50 = treat50*post) calc2(replace control75 = treat75*post)
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(7,2,.)
forvalues i = 1/6 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[7,2] = rmult[7,6]
local wy = rmult[7,6]

mat colnames rivals = control50 control75
mat rownames rivals = a1 a2 a3 a4 a5 a6 an

matrix list rivals


file open newfile  using "$tables/wyattspill.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'") 
file close newfile


foreach var in $envatt an {
  eststo xtrspill1_`var': xtreg `var' control50 control75 post, fe nonest vce(cluster club_id) 
  estadd matrix ri = rivals["`var'",1...]
   test control75=control50 
    local rp = round(r(p),0.01)
  estadd scalar joint `rp': xtrspill1_`var'
  sum `var' if treatment == 1 & post == 0
  local ymean = round(r(mean),0.01)
  estadd scalar s1 `ymean':  xtrspill1_`var'
  sum `var'
  local obs = r(N)
  estadd scalar o1 `obs':  xtrspill1_`var'

}

restore
		
esttab xtr_a1 xtr_a1 xtr_a3 xtr_a4 xtr_a5 xtr_a6  using "$tables/kidsattitudes.tex", replace se nonotes noobs style(tex) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
keep(treatindiv) nolines ///
coeflabels(treatindiv "Treated") nolines collabels(none) ///
		starlevels(* 0.10 ** 0.05 *** 0.01) label nomtitles ///
		nonumbers  fragment  ///
		prehead( ) ///
		posthead(\\ \midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrgen1_a1 xtrgen1_a2 xtrgen1_a3 xtrgen1_a4 xtrgen1_a5 xtrgen1_a6 using "$tables/kidsattitudes.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treatgender) nolines  collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatgender "Treated $\times$ Female") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrtime1_a1 xtrtime1_a2 xtrtime1_a3 xtrtime1_a4 xtrtime1_a5 xtrtime1_a6 using "$tables/kidsattitudes.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treattime) nolines collabels(none)  ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treattime "Treated $\times$ Time since treated") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\  ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrage1_a1 xtrage1_a2 xtrage1_a3 xtrage1_a4 xtrage1_a5 xtrage1_a6  using "$tables/kidsattitudes.tex", append se nonotes style(tex) noobs scalar("sb Mean control boys" "sg Mean control girls" "o1 Observations") sfmt(%12.3f %12.3f %12.0f) b(%12.3f) se(%12.3f) ///
keep(treatindiv treatage) nolines collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatage "Treated $\times$ Age") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot( \\ \midrule ) ///
		postfoot()
		
esttab xtrspill1_a1 xtrspill1_a2 xtrspill1_a3 xtrspill1_a4 xtrspill1_a5 xtrspill1_a6  using "$tables/kidsattitudes.tex", append se nonotes style(tex) label ///
noobs scalar("joint P-val joint test" "s1 Mean control" "o1 Observations") sfmt(%12.3f %12.3f %12.0f)  b(%12.3f) se(%12.3f) ///
keep(control50 control75) nolines collabels(none) ///
coeflabels(control50 "Control 50/50" control75 "Control 75/25") ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot(\\ \midrule) ///
		postfoot()


}

* 5. Mangrove attitudes table

if 0{		
estimates clear


preserve
gen mn = 0 // generate another variable as a placement column to add joint p-value for now. 
la var mn "Joint test"


drop if spillsample == 1
estimates clear

/*

RI: average treatment


*/
randcmd ///
((treatindiv) eststo xtr1: xtreg m1 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr2: xtreg m2 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr3: xtreg m3 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr4: xtreg m4 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr5: xtreg m5 treatindiv post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id) reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post)

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,1,.)
forvalues i = 1/5 {
		matrix rivals[`i',1] = rcoef[`i',6]
}

matrix rivals[6,1] = rmult[6,6]
local wy = rmult[6,6]

mat colnames rivals = treatindiv 
mat rownames rivals = m1 m2 m3 m4 m5 mn
matrix list rivals


file open newfile  using "$tables/wymatt.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'") 
file close newfile


foreach var in $manatt mn {
  eststo xtr_`var': xtreg `var' treatindiv post, fe nonest vce(cluster club_id)
   estadd matrix ri = rivals["`var'",1...]
}

/*

RI: treatment and time


*/
	
randcmd ///
((treatindiv treattime) eststo xtr1: xtreg m1 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr2: xtreg m2 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr3: xtreg m3 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr4: xtreg m4 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr5: xtreg m5 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id) reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treattime = timesincetreatment*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[6,2] = rmult[6,6]

mat colnames rivals = treatindiv treattime
mat rownames rivals = m1 m2 m3 m4 m5 mn
matrix list rivals

foreach var in $manatt mn  {
  eststo xtrtime1_`var': xtreg `var' treatindiv post treattime, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
  
  } 
  
/*

RI:  treatment and gender


*/ 
	
randcmd ///
((treatindiv treatgender) eststo xtr1: xtreg m1 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr2: xtreg m2 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr3: xtreg m3 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr4: xtreg m4 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr5: xtreg m5 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treatgender= dem_gender*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[6,2] = rmult[6,6]

mat colnames rivals = treatindiv treatgender
mat rownames rivals = m1 m2 m3 m4 m5 mn
matrix list rivals
  

foreach var in $manatt mn {
  eststo xtrgen1_`var': xtreg `var' treatindiv post treatgender, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
	}

	
/*

RI:  treatment and age


*/
randcmd ///
((treatindiv treatage) eststo xtr1: xtreg m1 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr2: xtreg m2 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr3: xtreg m3 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr4: xtreg m4 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr5: xtreg m5 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treatage = dem_age*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
		
}

matrix rivals[6,2] = rmult[6,6]

mat colnames rivals = treatindiv treatage
mat rownames rivals = m1 m2 m3 m4 m5 mn





foreach var in $manatt mn {
   
  eststo xtrage1_`var': xtreg `var' treatindiv post treatage, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]
  sum `var' if dem_gender == 0 & treatment == 1 & post == 0
  local ymeanboys = round(r(mean),0.01)
  estadd scalar sb `ymeanboys':  xtrage1_`var'
  sum `var' if dem_gender == 1 & treatment == 1 & post == 0
  local ymeangirls = round(r(mean),0.01)
  estadd scalar sg `ymeangirls':  xtrage1_`var'
  sum `var'
  local obs = r(N) 	
  estadd scalar o1 `obs': xtrage1_`var'
}
 
restore

/*

RI: spillovers


*/

preserve
keep if spillsample == 1 | treatment == 1
g mn = 0 
// Spillovers

randcmd ///
((control50 control75) eststo xtr1: xtreg m1 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr2: xtreg m2 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr3: xtreg m3 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr4: xtreg m4 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr5: xtreg m5 control50 control75 post, fe nonest vce(cluster club_id)) ///
, treatvars(treat50 treat75) groupvar(club_id)  reps(1000) seed(1234) calc1(replace control50 = treat50*post) calc2(replace control75 = treat75*post) 
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[6,2] = rmult[6,6]
local wy = rmult[6,6]

mat colnames rivals = control50 control75
mat rownames rivals = m1 m2 m3 m4 m5 mn
matrix list rivals


file open newfile  using "$tables/wymanattspill.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'") 
file close newfile

foreach var in $manatt mn {
  eststo xtrspill1_`var': xtreg `var' control50 control75 post, fe nonest vce(cluster club_id) 
  estadd matrix ri = rivals["`var'",1...]
   test control75=control50=0
    local rp = round(r(p),0.01)
  estadd scalar joint `rp': xtrspill1_`var'
  sum `var' if treatment == 1 & post == 0
  local ymean = round(r(mean),0.01)
  estadd scalar s1 `ymean':  xtrspill1_`var'
  sum `var'
  local obs = r(N)
  estadd scalar o1 `obs':  xtrspill1_`var'

}

restore
		
esttab xtr_m1 xtr_m2 xtr_m3 xtr_m4 xtr_m5 using "$tables/kidsmanattitudes.tex", replace se nonotes noobs style(tex) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
keep(treatindiv) nolines ///
coeflabels(treatindiv "Treated") nolines collabels(none) ///
		starlevels(* 0.10 ** 0.05 *** 0.01) label nomtitles ///
		nonumbers  fragment  ///
		prehead( ) ///
		posthead(\\ \midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrgen1_m1 xtrgen1_m2 xtrgen1_m3 xtrgen1_m4 xtrgen1_m5 using "$tables/kidsmanattitudes.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treatgender) nolines  collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatgender "Treated $\times$ Female") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrtime1_m1 xtrtime1_m2 xtrtime1_m3 xtrtime1_m4 xtrtime1_m5 using "$tables/kidsmanattitudes.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treattime) nolines collabels(none)  ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treattime "Treated $\times$ Time since treated") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\  ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrage1_m1 xtrage1_m2 xtrage1_m3 xtrage1_m4 xtrage1_m5 using "$tables/kidsmanattitudes.tex", append se nonotes style(tex) noobs scalar("sb Mean control boys" "sg Mean control girls" "o1 Observations") sfmt(%12.3f %12.3f %12.0f) b(%12.3f) se(%12.3f) ///
keep(treatindiv treatage) nolines collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatage "Treated $\times$ Age") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot( \\ \midrule) ///
		postfoot()
		
esttab xtrspill1_m1 xtrspill1_m2 xtrspill1_m3 xtrspill1_m4 xtrspill1_m5 using "$tables/kidsmanattitudes.tex", append se nonotes style(tex) label ///
 noobs scalar("joint P-val joint test" "s1 Mean control" "o1 Observations") sfmt(%12.3f %12.3f %12.0f)  b(%12.3f) se(%12.3f) ///
keep(control50 control75) nolines collabels(none) ///
coeflabels(control50 "Control 50/50" control75 "Control 75/25") ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot(\\ \midrule) ///
		postfoot()

}
		
* 6. Behaviors table

if 0{		


preserve
gen bn = 0 // generate another variable as a placement column to add joint p-value for now. 
la var bn "Joint test"

drop if spillsample == 1
estimates clear

/*

RI: average treatment


*/
randcmd ///
((treatindiv) eststo xtr1: xtreg b1 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr2: xtreg b2 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr3: xtreg b3 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr4: xtreg b4 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr5: xtreg b5 treatindiv post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)   reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post)

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,1,.)
forvalues i = 1/5 {
		matrix rivals[`i',1] = rcoef[`i',6]
}

matrix rivals[6,1] = rmult[6,6]
local wy = rmult[6,6]

mat colnames rivals = treatindiv 
mat rownames rivals = b1 b2 b3 b4 b5 bn
matrix list rivals

file open newfile  using "$tables/wybehav.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'") 
file close newfile



foreach var in $envbehav bn {
  eststo xtr_`var': xtreg `var' treatindiv post, fe nonest vce(cluster club_id)
   estadd matrix ri = rivals["`var'",1...]
}
 
/*

RI: treatment and time


*/
	
randcmd ///
((treatindiv treattime) eststo xtr1: xtreg b1 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr2: xtreg b2 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr3: xtreg b3 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr4: xtreg b4 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr5: xtreg b5 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)   reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treattime = timesincetreatment*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[6,2] = rmult[6,6]

mat colnames rivals = treatindiv treattime
mat rownames rivals = b1 b2 b3 b4 b5 bn
matrix list rivals

foreach var in $envbehav bn  {
  eststo xtrtime1_`var': xtreg `var' treatindiv post treattime, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
  
  } 
  
/*

RI:  treatment and gender


*/ 
	
randcmd ///
((treatindiv treatgender) eststo xtr1: xtreg b1 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr2: xtreg b2 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr3: xtreg b3 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr4: xtreg b4 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr5: xtreg b5 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)    reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treatgender= dem_gender*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[6,2] = rmult[6,6]

mat colnames rivals = treatindiv treatgender
mat rownames rivals = b1 b2 b3 b4 b5 bn
matrix list rivals
  

foreach var in $envbehav bn {
  eststo xtrgen1_`var': xtreg `var' treatindiv post treatgender, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
	}

	
/*

RI:  treatment and age


*/
randcmd ///
((treatindiv treatage) eststo xtr1: xtreg b1 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr2: xtreg b2 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr3: xtreg b3 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr4: xtreg b4 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr5: xtreg b5 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)    reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treatage = dem_age*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
		
}

matrix rivals[6,2] = rmult[6,6]

mat colnames rivals = treatindiv treatage
mat rownames rivals =b1 b2 b3 b4 b5 bn

foreach var in $envbehav bn {
   
  eststo xtrage1_`var': xtreg `var' treatindiv post treatage, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]
  sum `var' if dem_gender == 0 & treatment == 1 & post == 0
  local ymeanboys = round(r(mean),0.01)
  estadd scalar sb `ymeanboys':  xtrage1_`var'
  sum `var' if dem_gender == 1 & treatment == 1 & post == 0
  local ymeangirls = round(r(mean),0.01)
  estadd scalar sg `ymeangirls':  xtrage1_`var'
  sum `var'
  local obs = r(N) 	
  estadd scalar o1 `obs': xtrage1_`var'
}
 
restore

/*

RI: spillovers


*/

preserve
keep if spillsample == 1 | treatment == 1
g bn = 0 
// Spillovers with heterogeneity

randcmd ///
((control50 control75) eststo xtr1: xtreg b1 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr2: xtreg b2 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr3: xtreg b3 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr4: xtreg b4 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr5: xtreg b5 control50 control75 post, fe nonest vce(cluster club_id)) ///
, treatvars(treat50 treat75) groupvar(club_id)    reps(1000) seed(1234) calc1(replace control50 = treat50*post) calc2(replace control75 = treat75*post) 
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(6,2,.)
forvalues i = 1/5 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[6,2] = rmult[6,6]
local wy = rmult[6,6]

mat colnames rivals = control50 control75
mat rownames rivals = b1 b2 b3 b4 b5 bn
matrix list rivals


file open newfile  using "$tables/wybehavspill.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'") 
file close newfile


foreach var in $envbehav bn {
  eststo xtrspill1_`var': xtreg `var' control50 control75 post, fe nonest vce(cluster club_id) 
  estadd matrix ri = rivals["`var'",1...]
   test control75=control50=0
   local rp = round(r(p),0.01)
  estadd scalar joint `rp': xtrspill1_`var'
  sum `var' if treatment == 1 & post == 0
  local ymean = round(r(mean),0.01)
  estadd scalar s1 `ymean':  xtrspill1_`var'
  sum `var'
  local obs = r(N)
  estadd scalar o1 `obs':  xtrspill1_`var'

}

restore
		
esttab xtr_b1 xtr_b2 xtr_b3 xtr_b4 xtr_b5 using "$tables/kidsbehavior.tex", replace se nonotes noobs style(tex) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
keep(treatindiv) nolines ///
coeflabels(treatindiv "Treated") nolines collabels(none) ///
		starlevels(* 0.10 ** 0.05 *** 0.01) label nomtitles ///
		nonumbers  fragment  ///
		prehead( ) ///
		posthead(\\ \midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrgen1_b1 xtrgen1_b2 xtrgen1_b3 xtrgen1_b4 xtrgen1_b5 using "$tables/kidsbehavior.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treatgender) nolines  collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatgender "Treated $\times$ Female") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrtime1_b1 xtrtime1_b2 xtrtime1_b3 xtrtime1_b4 xtrtime1_b5 using "$tables/kidsbehavior.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treattime) nolines collabels(none)  ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treattime "Treated $\times$ Time since treated") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\  ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrage1_b1 xtrage1_b2 xtrage1_b3 xtrage1_b4 xtrage1_b5  using "$tables/kidsbehavior.tex", append se nonotes style(tex) noobs scalar("sb Mean control boys" "sg Mean control girls" "o1 Observations") sfmt(%12.3f %12.3f %12.0f) b(%12.3f) se(%12.3f) ///
keep(treatindiv treatage) nolines collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatage "Treated $\times$ Age") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot( \\ \midrule) ///
		postfoot()
		
esttab xtrspill1_b1 xtrspill1_b2 xtrspill1_b3 xtrspill1_b4 xtrspill1_b5 using "$tables/kidsbehavior.tex", append se nonotes style(tex) label noobs scalar("joint P-val joint test" "s1 Mean control" "o1 Observations") sfmt(%12.3f %12.3f %12.0f)  b(%12.3f) se(%12.3f) ///
keep(control50 control75) nolines collabels(none) ///
coeflabels(control50 "Control 50/50" control75 "Control 75/25") ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot(\\ \midrule) ///
		postfoot()


		
*Testing estimation for list experiment


xtreg countlitter1 i.treatindiv##i.listlitter1 i.post##i.listlitter1, fe nonest vce(cluster club_id) 
xtreg countlitter2 i.treatindiv##i.listlitter2 i.post##i.listlitter2,  fe nonest vce(cluster club_id) 
		
}
	
* 7. Summary table 

if 0{
	
preserve
gen yn = 0 // generate another variable as a placement column to add joint p-value for now. 
la var yn "Joint test"


drop if spillsample == 1
estimates clear

/*

RI: average treatment


*/
randcmd ///
((treatindiv) eststo xtr1: xtreg y5 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr2: xtreg a6 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr3: xtreg m1 treatindiv post, fe nonest vce(cluster club_id)) ///
((treatindiv) eststo xtr4: xtreg b5 treatindiv post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post)

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(5,1,.)
forvalues i = 1/4 {
		matrix rivals[`i',1] = rcoef[`i',6]
}

matrix rivals[5,1] = rmult[5,6]
local wy = rmult[5,6]

mat colnames rivals = treatindiv 
mat rownames rivals = y5 a6 m1 b5 yn

file open newfile  using "$tables/wysum.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'")  
file close newfile

foreach var in $summarytable yn {
  eststo xtr_`var': xtreg `var' treatindiv post, fe nonest vce(cluster club_id)
   estadd matrix ri = rivals["`var'",1...]
}

/*

RI: treatment and time


*/
	
randcmd ///
((treatindiv treattime) eststo xtr1: xtreg y5 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr2: xtreg a6 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr3: xtreg m1 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
((treatindiv treattime) eststo xtr4: xtreg b5 treatindiv treattime post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treattime = timesincetreatment*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(5,2,.)
forvalues i = 1/4 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[5,2] = rmult[5,6]

mat colnames rivals = treatindiv treattime
mat rownames rivals = y5 a6 m1 b5 yn
matrix list rivals

foreach var in $summarytable yn  {
  eststo xtrtime1_`var': xtreg `var' treatindiv post treattime, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
  
  } 
  
/*

RI:  treatment and gender


*/ 
	
randcmd ///
((treatindiv treatgender) eststo xtr1: xtreg y5 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr2: xtreg a6 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr3: xtreg m1 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
((treatindiv treatgender) eststo xtr4: xtreg b5 treatindiv treatgender post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treatgender= dem_gender*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(5,2,.)
forvalues i = 1/4 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[5,2] = rmult[5,6]

mat colnames rivals = treatindiv treatgender
mat rownames rivals = y5 a6 m1 b5 yn
matrix list rivals
  

foreach var in $summarytable yn {
  eststo xtrgen1_`var': xtreg `var' treatindiv post treatgender, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]	
	}

	
/*

RI:  treatment and age


*/
randcmd ///
((treatindiv treatage) eststo xtr1: xtreg y5 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr2: xtreg a6 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr3: xtreg m1 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
((treatindiv treatage) eststo xtr4: xtreg b5 treatindiv treatage post, fe nonest vce(cluster club_id)) ///
, treatvars(treatindividual) groupvar(club_id)  reps(1000) seed(1234) calc1(replace treatindiv = treatindividual*post) calc2(replace treatage = dem_age*treatindividual*post)	
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(5,2,.)
forvalues i = 1/4 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
		
}

matrix rivals[5,2] = rmult[5,6]

mat colnames rivals = treatindiv treatage
mat rownames rivals = y5 a6 m1 b5  yn 	

foreach var in $summarytable yn {
   
  eststo xtrage1_`var': xtreg `var' treatindiv post treatage, fe nonest vce(cluster club_id)
  estadd matrix ri = rivals["`var'",1...]
  sum `var' if dem_gender == 0 & treatment == 1 & post == 0
  local ymeanboys = round(r(mean),0.01)
  estadd scalar sb `ymeanboys':  xtrage1_`var'
  sum `var' if dem_gender == 1 & treatment == 1 & post == 0
  local ymeangirls = round(r(mean),0.01)
  estadd scalar sg `ymeangirls':  xtrage1_`var'
  sum `var'
  local obs = r(N) 	
  estadd scalar o1 `obs': xtrage1_`var'
}
 
restore

/*

RI: spillovers


*/

preserve
keep if spillsample == 1 | treatment == 1
g yn = 0 
// Spillovers with heterogeneity

randcmd ///
((control50 control75) eststo xtr1: xtreg y5 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr2: xtreg a6 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr3: xtreg m1 control50 control75 post, fe nonest vce(cluster club_id)) ///
((control50 control75) eststo xtr4: xtreg b5 control50 control75 post, fe nonest vce(cluster club_id)) ///
, treatvars(treat50 treat75) groupvar(club_id)  reps(1000) seed(1234) calc1(replace control50 = treat50*post) calc2(replace control75 = treat75*post)
		

matrix list e(RCoef) //  Randomization-c and -t p-values for individual coefficients.
matrix list e(RMult) //  Randomization-c and -t p-values for the Westfall-Young multiple testing 

// rand t p-value for each column
matrix rcoef = e(RCoef)

// WY p-value for last column 
matrix rmult = e(RMult)

matrix rivals = J(5,2,.)
forvalues i = 1/4 {
		scalar odd = 2*`i' - 1   // odd placement across rows for treat
		local j = odd
		matrix rivals[`i',1] = rcoef[`j',6]
		scalar even = 2*`i'  // even placement across rows for treat*time
		local k = even
		matrix rivals[`i',2] = rcoef[`k',6]
}

matrix rivals[5,2] = rmult[5,6]
local wy = rmult[5,6]

mat colnames rivals = control50 control75
mat rownames rivals = y5 a6 m1 b5  yn 	
matrix list rivals


file open newfile  using "$tables/wysumspill.tex", write replace 
local wy: di %4.3f `wy'
file write newfile ("`wy'") 
file close newfile

foreach var in $summarytable yn {
  eststo xtrspill1_`var': xtreg `var' control50  control75  post, fe nonest vce(cluster club_id) 
  estadd matrix ri = rivals["`var'",1...]
  test control50 = control75=0
  local rp = round(r(p),0.01)
  estadd scalar joint `rp': xtrspill1_`var'
  sum `var' if treatment == 1 & post == 0 
  local ymean = round(r(mean),0.01)
  estadd scalar s1 `ymean':  xtrspill1_`var'
  sum `var'
  local obs = r(N)
  estadd scalar o1 `obs':  xtrspill1_`var'

}

restore
		
esttab xtr_m1 xtr_y5 xtr_a6  xtr_b5 using "$tables/summarykids.tex", replace se nonotes noobs style(tex) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
keep(treatindiv) nolines ///
coeflabels(treatindiv "Treated") nolines collabels(none) ///
		starlevels(* 0.10 ** 0.05 *** 0.01) label nomtitles ///
		nonumbers  fragment  ///
		prehead( ) ///
		posthead(\\ \midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrgen1_m1 xtrgen1_y5 xtrgen1_a6  xtrgen1_b5 using "$tables/summarykids.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treatgender) nolines  collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatgender "Treated $\times$ Female") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrtime1_m1 xtrtime1_y5 xtrtime1_a6  xtrtime1_b5 using "$tables/summarykids.tex", append se nonotes style(tex) noobs b(%12.3f) se(%12.3f) ///
keep(treatindiv treattime) nolines collabels(none)  ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treattime "Treated $\times$ Time since treated") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\  ) ///
		posthead(\midrule \\) ///
		prefoot() ///
		postfoot()
		
esttab xtrage1_m1 xtrage1_y5 xtrage1_a6  xtrage1_b5 using "$tables/summarykids.tex", append se nonotes style(tex) noobs scalar("sb Mean control boys" "sg Mean control girls" "o1 Observations") sfmt(%12.3f %12.3f %12.0f) b(%12.3f) se(%12.3f) ///
keep(treatindiv treatage) nolines collabels(none) ///
cell(b(fmt(a3) star pvalue(ri)) se(fmt(a3) par) ri(fmt(a3) par([ ])))  ///
coeflabels(treatindiv "Treated" treatage "Treated $\times$ Age") ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot( \\ \midrule) ///
		postfoot()
		
esttab xtrspill1_m1 xtrspill1_y5 xtrspill1_a6  xtrspill1_b5 using "$tables/summarykids.tex", append se nonotes style(tex) label ///
 scalar("joint P-val joint test" "s1 Mean control" "o1 Observations") sfmt(%12.3f %12.3f %12.0f)  b(%12.3f) se(%12.3f) ///
keep(control50 control75) nolines collabels(none) ///
coeflabels(control50 "Control 50/50" control75 "Control 75/25") ///
cell(b(fmt(a3)) se(fmt(a3) par star) ri(fmt(a3) par([ ]) pvalue(rivals) star))  ///
		starlevels(* 0.10 ** 0.05 *** 0.01) nomtitles ///
		nonumbers  fragment  ///
		prehead( \\ ) ///
		posthead(\midrule \\) ///
		prefoot(\\ \midrule) ///
		postfoot()
	
}	


* 8. Figures for summary kids -- average effects and by gender


if 1{
	

foreach var in $summarytable  {
	
	xtreg `var' treatindiv post, fe nonest vce(cluster club_id)
	estimates store b`var'
}	

	
	coefplot (bm1,asequation("Prefer more mangroves") \ by5, asequation("Knowledge index") \ ba6, asequation("Attitudes index") \ bb5, asequation("Behavior index")) ,  msize(vlarge) xline(0) ciopts(recast(rcap)) ///  
	xtitle("Treatment effect") keep(treatindiv)  aseq swapnames 
	graph export "${figures}\kidsavgmfx.png", replace	
	
mat A = J(4,3,.)				// matrix to store treatment results for male
mat B = J(4,3,.)				// matrix to store treatment results for female
	

local n = 1
foreach var in $summarytable  {

 eststo t`var':  xtreg `var' i.treatindiv##i.dem_gender post, fe nonest vce(cluster club_id)
	mat A[`n',1] = r(table)[1,2]		// coefficient of 1.treat
	mat A[`n',2] = r(table)[5,2]		// lower CI boundary of 1.treat
	mat A[`n',3] = r(table)[6,2]		// upper CI boundary of 1.treat
	lincom 	1.treatindiv +1.treatindiv#1.dem_gender	
	mat B[`n',1] = r(estimate)		// coefficient of 1.treat+1.treat#1.female
	mat B[`n',2] = r(lb)			// lower CI boundary 
	mat B[`n',3] = r(ub)			// upper CI boundary
	local n = `n'+1	  

}

  coefplot (mat(A[.,1]), ci((A[,2] A[,3])) msize(vlarge) pstyle(p3) ciopts(recast(rcap))) ///
  (mat(B[.,1]), ci((B[,2] B[,3])) msize(vlarge) pstyle(p2) ciopts(recast(rcap))),  xline(0)		///
  	coeflab(r1="Prefer more mangroves" r2="Knowledge index" r3="Attitudes index" r4="Behavior index") 				///
	plotlab("Male" "Female")
graph export "${figures}\kidsgender.png", replace
}

 